// $URL: http://mingw-lib.googlecode.com/svn/trunk/working/browser_463/extra/nbr.cpp $
// $Rev: 190 $
// $Author: akio.miyoshi $
// $Date:: 2010-07-06 07:26:06 +0000#$

#include "nbr.h"

#include <QtCore>
#include <QtGui>
#include <QtNetwork>

NetworkBatchRequest::NetworkBatchRequest(QNetworkReply *a_reply, QByteArray *a_response, bool a_headerOnly)
    : m_reply(a_reply)
    , m_response(a_response)
    , m_headerOnly(a_headerOnly)
	, m_finished(false)
	, m_read_ready(false)
{

	connect(m_reply, SIGNAL(metaDataChanged()), this, SLOT(metaDataChanged()));
	connect(m_reply, SIGNAL(readyRead()), this, SLOT(replyReadyRead()));
	connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished()));
	if(m_response) m_response->clear();
}

void NetworkBatchRequest::metaDataChanged()
{
	qDebug() << ("NetworkBatchRequest::metaDataChanged()");
}

void NetworkBatchRequest::replyReadyRead()
{
	qDebug() << ("NetworkBatchRequest::replyReadyRead()");
	m_read_ready = true;
	if(m_headerOnly)
    {
        this->replyFinished();
    }
    else
    {
        QByteArray bytes = m_reply->readAll();
		if(m_response) m_response->append(bytes);
        qDebug() << "NetworkBatchRequest::replyReadyRead()=====>bytes.size():" << bytes.size() << m_reply->rawHeader("Content-Type");
        QByteArray v_lengthRaw = m_reply->rawHeader("Content-Length");
        if(v_lengthRaw != "")
        {
            int v_length = v_lengthRaw.toInt();
			int v_read = m_response ? m_response->size() : 0;
            double v_pct = (v_read * 1.0)/v_length;
            v_pct *= 100;
            qDebug() << QString("NetworkBatchRequest::replyReadyRead()=====>%L1/%L2(%3%%)").arg(v_read).arg(v_length).arg(v_pct);
        }
#if 0x1
		if(m_wait_type == NetworkBatchRequest::ReadyRead)
		{
			m_eventLoop.exit(0);
		}
#endif
    }
}

void NetworkBatchRequest::replyFinished()
{
    qDebug() << ("NetworkBatchRequest::replyFinished()");
	m_finished = true;
    if(m_headerOnly)
    {
        m_reply->abort();
    }
    else
    {
        QList<QByteArray> headerList = m_reply->rawHeaderList();
		if(headerList.contains("Location") && m_response)
        {
            m_response->clear();
            QByteArray v_locationRaw = m_reply->rawHeader("Location");
            qDebug() << "[v_locationRaw]" << v_locationRaw;
            m_response->append(v_locationRaw);
        }
    }
    disconnect(m_reply, 0, this, 0);
    m_eventLoop.exit(0);
}

void NetworkBatchRequest::wait()
{
	m_wait_type = NetworkBatchRequest::Finished;
	m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
}

bool NetworkBatchRequest::waitWithType(WaitType a_wait_type)
{
	m_wait_type = a_wait_type;
	if(m_wait_type == NetworkBatchRequest::ReadyRead)
	{
		if(m_finished)
		{
			return (m_response->size() > 0);
		}
		if(m_read_ready)
		{
			if(m_response->size() > 0) return true;
			//else go into event loop
			m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
			return (m_response->size() > 0);
		}
	}
	m_eventLoop.exec(QEventLoop::ExcludeUserInputEvents);
	return true;
}

QByteArray NetworkBatchRequest::read(qint64 maxSize)
{
	if(!m_response) return QByteArray();
	qDebug() << "[NetworkBatchRequest::read()]" << m_response->size();
	QBuffer v_buffer;
	v_buffer.setBuffer(m_response);
	v_buffer.open(QBuffer::ReadOnly);
	QByteArray v_read = v_buffer.read(maxSize);
	v_buffer.close();
	m_response->remove(0, v_read.size());
	qDebug() << "[NetworkBatchRequest::read()]" << m_response->size();
	return v_read;
}
